package src.queries;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;

import  src.entities.User;


public class UploadQueries extends UnsecuredQueries{

	public UploadQueries(Connection c, SecurityQueries security) {
		super(c, security);
	}

	public void createRadRecord( String patientName, 
			String doctorName, String radiologistName, 
			String testType, java.util.Date prescribeDate, java.util.Date testDate, 
			String diagnosis, String description) throws SQLException{
		
		//TODO Should we check here if the radiologist is actually a radiologist? or should we
		//simply insert the name of the radiologist entering the test record
		
			java.sql.Date sqlPrescribeDate = new java.sql.Date(prescribeDate.getTime());
			java.sql.Date sqlTestDate = new java.sql.Date(testDate.getTime());
		
			PreparedStatement p = makePreparedStatement(
			"INSERT INTO radiology_record VALUES (record_seq.nextval,?,?,?,?,?,?,?,?)" );

			p.setString(1, patientName);
			p.setString(2, doctorName);
			p.setString(3, radiologistName);
			p.setString(4, testType);
			p.setDate(5, sqlPrescribeDate);
			p.setDate(6, sqlTestDate);
			p.setString(7, diagnosis);
			p.setString(8, description);

			p.executeQuery();
			
			PreparedStatement pcom = makePreparedStatement("commit");
			pcom.executeQuery();
		
	}
	public void addPacsImage(int recordID,
			File thumbnailFile, File regularSizeFile, File fullSizeFile) throws SQLException, IOException{
		
		InputStream thumbnail = new FileInputStream(thumbnailFile);
		InputStream regular = new FileInputStream(regularSizeFile);
		InputStream full = new FileInputStream(fullSizeFile);
		
		PreparedStatement p1 = makePreparedStatement("SELECT pic_id_sequence.nextval from dual");
		ResultSet rs1 = p1.executeQuery();
		rs1.next();
		int pic_id = rs1.getInt(1);
		
		PreparedStatement p2 = makePreparedStatement("INSERT INTO pacs_images VALUES(1,?,empty_blob(), empty_blob(), empty_blob())");
		p2.setInt(1, pic_id);
		p2.executeQuery();
		
		PreparedStatement p3 = makePreparedStatement("SELECT * FROM pacs_images WHERE image_id = "+pic_id+" FOR UPDATE");
	    ResultSet rs2 = p3.executeQuery();
	    rs2.next();
	    BLOB thumbBlob = ((OracleResultSet)rs2).getBLOB(3);
	    BLOB regularBlob = ((OracleResultSet)rs2).getBLOB(3);
	    BLOB fullBlob = ((OracleResultSet)rs2).getBLOB(3);
	    
	    OutputStream thumboutstream = thumbBlob.getBinaryOutputStream();
	    int thumbSize = thumbBlob.getBufferSize();
	    byte[] thumbBuffer = new byte[thumbSize];
	    writeToBlob(thumboutstream,thumbSize,thumbBuffer,thumbnail);
	    
	    OutputStream regularoutstream = regularBlob.getBinaryOutputStream();
	    int regularSize = regularBlob.getBufferSize();
	    byte[] regularBuffer = new byte[regularSize];
	    writeToBlob(regularoutstream, regularSize, regularBuffer, regular);
	    
	    OutputStream fulloutstream = fullBlob.getBinaryOutputStream();
	    int fullSize = fullBlob.getBufferSize();
	    byte[] fullBuffer = new byte[fullSize];
	    writeToBlob(fulloutstream, fullSize, fullBuffer, full);
	    
	    p3.executeUpdate("commit");
	    
	    PreparedStatement pcom = makePreparedStatement("commit");
		pcom.executeUpdate();
		
	}
	
	public void writeToBlob(OutputStream out, int blobSize, byte[] buffer, InputStream in) throws IOException{
		 	int length = -1;
		    
		    while((length = in.read(buffer)) != -1){
		    	out.write(buffer, 0, length);
		    }
		    in.close();
		    out.close();
	}

}
